import datetime
from django.db import models
from django.utils.timezone import now

weekStr = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]


class Tag(models.Model):
    name = models.CharField(max_length=10, verbose_name="标签名", null=False, default="")

    class Meta:
        verbose_name = "标签"
        verbose_name_plural = "标签"

    def __str__(self):
        return self.name


class Lecture(models.Model):
    title = models.CharField(max_length=255, verbose_name="主题")
    teacher = models.CharField(max_length=255, verbose_name="主讲人")
    classroom = models.CharField(max_length=255, verbose_name="地点")
    tag = models.ManyToManyField(to=Tag, verbose_name="标签", blank=False)
    department = models.CharField(
        max_length=255, verbose_name="主办单位", blank=True, null=False, default=""
    )
    date = models.DateField(verbose_name="日期", default=now)
    startTime = models.TimeField(verbose_name="开始时间", default=datetime.time(9, 0))
    endTime = models.TimeField(verbose_name="结束时间", default=datetime.time(11, 0))
    startIndex = models.IntegerField(verbose_name="开始节数", default=0)
    endIndex = models.IntegerField(verbose_name="结束节数", default=0)
    accurate = models.BooleanField(verbose_name="是否准确", default=False)
    info = models.TextField(
        max_length=1000, verbose_name="讲座内容", blank=True, null=False, default=""
    )
    special = models.TextField(
        max_length=255, verbose_name="特殊要求", blank=True, null=False, default="无"
    )
    other = models.TextField(
        max_length=1000, verbose_name="备注信息", blank=True, null=False, default="无"
    )
    slug = models.CharField(max_length=10, verbose_name="语雀路径", null=False, default="")
    docID = models.IntegerField(verbose_name="语雀id", default=0)
    count = models.IntegerField(verbose_name="计数", default=0)

    class Meta:
        verbose_name = "讲座"
        verbose_name_plural = "讲座"

    def __str__(self):
        return self.title

    @property
    def tag_list(self):
        if hasattr(self, "tag_list_str"):
            return self.tag_list_str
        else:
            return [i.name for i in self.tag.all()]

    @property
    def markdown(self):
        return f"""**主讲人**：{self.teacher}

**地点**：{self.classroom}

**标签**：{'，'.join(self.tag_list)}

**时间**：{self.date}（{weekStr[self.date.weekday()]}） {self.startTime.strftime('%H:%M')}-{self.endTime.strftime('%H:%M')}

**讲座内容**：{self.info}

**其他信息**：{self.other}

**特殊要求**：{self.special}

**主办单位**：{self.department}
"""
